#include <config.h>
#include <string.h>

#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

#include <stdio.h>

#ifndef HAVE_GETHOSTBYNAME_R
pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
int gethostbyname_r (__const char *name,
		struct hostent *result_buf,
		char *buf, size_t buflen,
		struct hostent **result,
		int *h_errnop) {
	int ret = 0;
	if(!pthread_mutex_lock(&ghbn_mutex)) {
		struct hostent *he;
		//printf("name: %s\n", name);
		he = gethostbyname(name);
		if(he) {
			//printf("ok\n");
			memcpy(result_buf, he, sizeof(struct hostent));
		} else {
			ret = h_errno;
		}
		pthread_mutex_unlock(&ghbn_mutex);
		return ret;
	}
	return errno ? errno : 1;
}

#endif

#ifndef HAVE_STRNDUP

char *strndup(const char *ori, size_t n) {
	size_t on = strlen(ori);
	char *ret;
	if(on < n) {
		return strdup(ori);
	}
	ret = malloc(n);
	if(ret) {
		memcpy(ret, ori, n - 1);
		ret[n-1] = '\0';
	}
	return ret;

}

#endif
